package com.verisette.ncos.service.card.bizunit;

import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterTemplate;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;
import com.verisette.ncos.web.card.bean.CardCreateBean;

public class CreatePDFNameBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(CreatePDFNameBU.class);
	
	private static final String[] PRINT_TYPE_FLAG = {"S", "SE", "D"};
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );
		
		CardCreateBean cardCreateBean = (CardCreateBean)processContext.getValueObject();
		TransCustomerOrder order = cardCreateBean.getOrder();
		
		if(logger.isDebugEnabled()){
			StringBuilder inputBd = new StringBuilder();
			inputBd.append("Input:\n\t\t");
			inputBd.append(cardCreateBean);
			logger.debug(inputBd.toString());
		}
		try{
			String printTypeName = PRINT_TYPE_FLAG[Integer.valueOf(cardCreateBean.getPrintType())];
			MasterTemplate template = (MasterTemplate)commonDAO.findById(MasterTemplate.class, order.getMasterTemplate().getMtpId());
			order.setMasterTemplate(template);
			order.setProductCode(String.format("%s/%s", template.getMtpShortName(), printTypeName));

			String filePDFFormat = ProjectConstant.FILE_PDF_FORMAT.format(order.getCreatedDate());
			String pdfFileName = filePDFFormat
									.replaceAll(ProjectConstant.PARAM_MTPSHORTNAME, template.getMtpShortName())
									.replaceAll(ProjectConstant.PARAM_PRINTTYPE, printTypeName);
			order.setPdfPath(pdfFileName);
			cardCreateBean.setOrder(order);
		}catch(Exception ex){
			logger.error(ex.getStackTrace());
			ex.printStackTrace();
		}
		
		processContext.setValueObject(cardCreateBean);
		processContext.getOriginalRequest().setValueObject(cardCreateBean);
	}
}